package com._54programer.config;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.tio.utils.json.Json;

import com._54programer.entity.User;
import com._54programer.ws.Const;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;

import lombok.extern.slf4j.Slf4j;

/**
 * 启动时加载(异步加载)
 * 
 * @author Administrator
 *
 */
@Slf4j
@Component
public class HazelcastRunner implements CommandLineRunner {

	public static volatile HazelcastInstance ExpHi;

	@Autowired
	private HazelcastInstance Hi;

	@Override
	public void run(String... args) throws Exception {
		CompletableFuture.runAsync(() -> {
			//注册对外HazelcastInstance
			ExpHi = Hi;
			
			//创建机器人用户
			IMap<Object, Object> robots = Hi.getMap(Const.ROBOT);
			User user = new User();
			user.setUid("124568923");
			user.setUsername("zhz001");
			user.setPassword("123456");
			robots.put(user.getUsername(), Json.toJson(user));
			
			User user2 = new User();
			user2.setUid("658794485");
			user2.setUsername("zhz002");
			user2.setPassword("123456");
			robots.put(user2.getUsername(), Json.toJson(user2));

			List list = new ArrayList<>();
			for (Object key : Hi.getMap(Const.ROBOT).keySet()) {
				list.add(key);
			}
			log.info("初始化机器仁人： {}",list);
			log.info("初始化机器仁人： {}",Hi.getMap(Const.ROBOT).values());
			
		}).toCompletableFuture();
	}
}
